#include "qcomboboxitemdelegate.h"
#include <QComboBox>
#include <QTableView>
#include <QStandardItemModel>

#include <QSqlQuery>



QComboBoxItemDelegate::QComboBoxItemDelegate(QWidget *parent)
    : QItemDelegate(parent)
{

}

QComboBoxItemDelegate::~QComboBoxItemDelegate()
{

}

QWidget* QComboBoxItemDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem & option ,
    const QModelIndex & index ) const
{
    QModelIndex tempIndex;
    tempIndex = index.sibling(index.row(), 1);
    QString value = index.model()->data(tempIndex, Qt::DisplayRole).toString();
    CHelperClass *hlp = new CHelperClass(NULL);


    QComboBox *editor = NULL;
    if ((index.column() == 2) && hlp->isArtikalGrouped(value))
    {
        editor = new QComboBox(parent);
        const QModelIndex temp = index;
        QString value = index.model()->data(index, Qt::DisplayRole).toString();
        QStringList list ;
		QStringList listKey;
		hlp->getListofArtikalGrouped(artMap, listKey);
        editor->addItems(listKey);
        editor->installEventFilter(const_cast<QComboBoxItemDelegate*>(this));
    }
    delete hlp;
    return editor;
}

void QComboBoxItemDelegate::setEditorData(QWidget *editor,
    const QModelIndex &index) const
{

    QString value = index.model()->data(index, Qt::DisplayRole).toString();
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    //comboBox->addItem(value);
   
    int index1 = comboBox->findText(value);
    if ( index1 != -1 ) 
    { // -1 for not found
        comboBox->setCurrentIndex(index1);
    }
}

void QComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
{
    QLocale loc;
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    int selectedItem = comboBox->findText(comboBox->currentText());
    QString value = comboBox->currentText();
    model->setData(index, value);
    std::map<QString, tArt>::iterator iter = artMap.find(value);
    QModelIndex topLeft;
    
    if(iter != artMap.end())
    {

        topLeft = model->index(index.row(), 0, QModelIndex());
        model->setData(topLeft, (*iter).second.art_id);
        
        topLeft = model->index(index.row(), 1, QModelIndex());
        model->setData(topLeft, (*iter).second.art_sifra);

        topLeft = model->index(index.row(), 2, QModelIndex());
        model->setData(topLeft, (*iter).second.art_name);

        topLeft = model->index(index.row(), 3, QModelIndex());
        model->setData(topLeft, (*iter).second.art_edm);

        CHelperClass *m_chc = new CHelperClass(NULL);
        m_chc->createSaldoMap(0);    

        QString item_sifra = (*iter).second.art_id;
        float saldo = m_chc->getLagerLista(item_sifra , (QString)"ART_PRILIV") - 
            m_chc->getLagerLista(item_sifra , (QString)"ART_ODLIV"); 
        delete m_chc;
        
        topLeft = model->index(index.row(), 5, QModelIndex());
        model->setData(topLeft, loc.toString(saldo, 'f', 2));

        topLeft = model->index(index.row(), 4, QModelIndex());
        float kolvk = index.model()->data(topLeft, Qt::DisplayRole).toFloat();
        
        
        if (saldo >= kolvk)
        {
            QIcon ic1(":/sterna/Resources/ok.png"); 
            topLeft = model->index(index.row(), 6, QModelIndex());
            model->setData(topLeft, ic1, Qt::DecorationRole);    
        }
        else
        {
            QIcon ic1(":/sterna/Resources/stop.png");
            topLeft = model->index(index.row(), 6, QModelIndex());
            model->setData(topLeft, ic1, Qt::DecorationRole);
        }
    }
    emit updatePodatoci();
}

void QComboBoxItemDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    QRect re = option.rect;
    editor->setGeometry(re);
}

void QComboBoxItemDelegate::setAFont(QFont font)
{
}

void QComboBoxItemDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index )
{
    int i = 1;  
}

void QComboBoxItemDelegate::setKolicinaMusterija(float kolMusterija)
{
    m_kolMusterija = kolMusterija;
}